package com.oilsection.demo.service.impl;

import cn.hutool.core.date.DateUtil;
import com.oilsection.demo.controller.res.JcEventRightPanelResponse;
import com.oilsection.demo.dao.*;
import com.oilsection.demo.po.*;
import com.oilsection.demo.entity.specKqInfo;
import com.oilsection.demo.entity.specKqPointInfo;
import com.oilsection.demo.service.JcEventService;
import com.oilsection.demo.service.QkListService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yangyuge
 * @description
 * @Date 2024/5/3 16:13
 */
@Service
public class QkListServiceImpl implements QkListService {
    private static final Logger logger = LoggerFactory.getLogger(QkListServiceImpl.class);

    @Autowired
    private GetQkListSourceDao getQkListSourceDao;

    @Autowired
    private GetJzListSourceDao getJzListSourceDao;

    @Autowired
    private GetJcdListSourceDao getJcdListSourceDao;

    @Autowired
    private GetXldListSourceDao getXldListSourceDao;

    @Autowired
    private GetCkListSourceDao getCkListSourceDao;

    @Autowired
    private GetHkListSourceDao getHkListSourceDao;

    @Autowired
    private GetJkxlListSourceDao getJkxlListSourceDao;

    @Autowired
    private JcEventService jcEventService;

    @Override
    public List<KqInfoTable> getQkList() {
        List<KqInfoTable> kqInfoTables = getQkListSourceDao.findAll();
        return kqInfoTables;
    }

    @Override
    public Map<String, specKqInfo> getSpecKqInfo(String id) {
        KqInfoTable kqInfoTable = getQkListSourceDao.findById(id).get();

        Map<String, specKqInfo> resultMap = new HashMap<>();
        specKqInfo res = new specKqInfo();
        res.setId(kqInfoTable.getId());
        res.setArea(kqInfoTable.getArea());
        res.setName(kqInfoTable.getName());
        res.setJzNum(kqInfoTable.getJzNum());
        res.setJcdNum(kqInfoTable.getJcdNum());
        res.setKqState(kqInfoTable.getKqState());
        res.setKqxlNum(kqInfoTable.getXlkNum());
        res.setCqxlNum(kqInfoTable.getCqkNum());
        res.setHkxlNum(kqInfoTable.getHkNum());
        List<Map<String, String>> qkXlList = new ArrayList<Map<String, String>>();
        List<Map<String, String>> cqkXlList = new ArrayList<Map<String, String>>();
        List<Map<String, String>> hkXlList = new ArrayList<Map<String, String>>();
        List<Map<String, String>> jzList = new ArrayList<Map<String, String>>();
        List<Map<String,String>> jkxlList = new ArrayList<>();
        List<Map<String, List<String>>> jzMapList = new ArrayList<Map<String, List<String>>>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        int jkxlNum = 0;
        List<JzInfoTable> jzInfoTable = getJzListSourceDao.findByKqId(id);
        for (JzInfoTable list : jzInfoTable) {
            Map<String, String> map = new HashMap<>();
            Map<String, String> mapQk = new HashMap();
            Map<String, String> mapCk = new HashMap();
            Map<String, String> mapHk = new HashMap();
            Map<String,String> mapJk = new HashMap<>();

            map.put("jz_name", list.getJzName());
            String d = sdf.format(list.getUpdateTime());
            map.put("update_time", d);
            jzList.add(map);


            mapQk.put("name", list.getJzName());
            mapQk.put("value", String.valueOf(list.getXldNum()));
            qkXlList.add(mapQk);

            mapCk.put("name", list.getJzName());
            mapCk.put("value", String.valueOf(list.getCkNum()));
            cqkXlList.add(mapCk);

            mapHk.put("name", list.getJzName());
            mapHk.put("value", String.valueOf(list.getHkNum()));
            hkXlList.add(mapHk);

            List<JkxlInfoTable> jkxlInfoTables = new ArrayList<>();
            jkxlInfoTables = getJkxlListSourceDao.findByXljzIdAndDealSection(list.getId(),"未处理");
            mapJk.put("name",list.getJzName());
            mapJk.put("value",String.valueOf(jkxlInfoTables.size()));
            jkxlNum += jkxlInfoTables.size();
            jkxlList.add(mapJk);
        }

        res.setJkxlNum(jkxlNum);
        res.setJzList(jzList);
        res.setKqxlList(qkXlList);
        res.setCqxlList(cqkXlList);
        res.setHkxlList(hkXlList);
        res.setJkxlList(jkxlList);
        resultMap.put("data", res);
        return resultMap;

    }

    //特定库区井组和检测点坐标
    @Override
    public List<specKqPointInfo> getSpecKqPoint(String id) {
        List<JzInfoTable> jzInfoTables = getJzListSourceDao.findByKqId(id);
        List<specKqPointInfo> res = new ArrayList<>();
        for (JzInfoTable ele : jzInfoTables) {
            specKqPointInfo specKqPointInfo = new specKqPointInfo();
            specKqPointInfo.setJzId(ele.getId());
            specKqPointInfo.setJzXpoint(ele.getJkXpoint());
            specKqPointInfo.setJzYpoint(ele.getJkYpoint());
            List<JcdInfoTable> jcdInfoTables = getJcdListSourceDao.findByJzId(ele.getId());
            List<Map<String, String>> jcdList = new ArrayList<>();
            for (JcdInfoTable item : jcdInfoTables) {
                Map<String, String> map = new HashMap<>();
                map.put("jcdId", item.getId());
                map.put("jcdXpoint", item.getJcdXpoint());
                map.put("jcdYpoint", item.getJcdYpoint());
                jcdList.add(map);
            }
            specKqPointInfo.setJcdPointList(jcdList);
            res.add(specKqPointInfo);
        }

        return res;
    }

    //库区对应的井组
    @Override
    public List<Map<String, String>> getSpecKqJzPoint(String id) {
        List<Map<String, String>> res = new ArrayList<>();
        List<JzInfoTable> jzInfoTables = getJzListSourceDao.findByKqId(id);

        for (JzInfoTable ele : jzInfoTables) {
            Map<String, String> map = new HashMap<>();
            map.put("jzId", ele.getId());
            map.put("jzName", ele.getJzName());
            map.put("jzState", ele.getJzState());
            map.put("jzXpoint", ele.getJkXpoint());
            map.put("jzYpoint", ele.getJkYpoint());
            map.put("jzName", ele.getJzName());

            res.add(map);
        }
        return res;
    }

    //井组对应的监测点
    @Override
    public List<Map<String, String>> getSpecKqJcdPoint(String id) {
        List<Map<String, String>> res = new ArrayList<>();
        List<JcdInfoTable> jcdInfoTables = getJcdListSourceDao.findByJzId(id);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        for (JcdInfoTable ele : jcdInfoTables) {
            Map<String, String> map = new HashMap<>();
            map.put("jcdId", ele.getId());
            map.put("jcdXpoint", ele.getJcdXpoint());
            map.put("jcdYpoint", ele.getJcdYpoint());
            String d = sdf.format(ele.getUpdateTime());
            map.put("updateTime", d);
            map.put("jcValue", String.valueOf(ele.getUpdateValue()));
            map.put("jcdName", ele.getJcdName());
            res.add(map);
        }
        return res;
    }

    //井组对应的泄露点
    @Override
    public List<Map<String, String>> getSpecXld(String id) {
        List<Map<String, String>> res = new ArrayList<>();
        List<XldInfoTable> xldInfoTables = getXldListSourceDao.findByJzId(id);
        for (XldInfoTable ele : xldInfoTables) {
            Map<String, String> map = new HashMap<>();
            map.put("xldId", ele.getId());
            map.put("xldXpoint", ele.getXldcXpoint());
            map.put("xldYpoint", ele.getXldcYpoint());
            map.put("dealSection", ele.getDealSection());
            res.add(map);
        }
        return res;
    }

//    @Override
//    public List<Map<String,String>> getSpecCk(String id){
//        List<Map<String,String>> res = new ArrayList<>();
//        List<ckInfoTable> ckInfoTables = getCkListSourceDao.findByCkyId(id);
//        for(ckInfoTable ele: ckInfoTables){
//            Map<String,String> map= new HashMap<>();
//            map.put("jzId", ele.getCkyId());
//        }
//
//        return res;
//    }

    @Override
    public Map<String, List<Map<String, String>>> getOverviewView(String id) {
        Map<String, List<Map<String, String>>> res = new HashMap<>();
        List<JzInfoTable> jzInfoTables = getJzListSourceDao.findByKqId(id);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        List<Map<String, String>> jcdList = new ArrayList<>();
        List<Map<String, String>> xlList = new ArrayList<>();
        List<Map<String, String>> ckList = new ArrayList<>();
        List<Map<String, String>> hkList = new ArrayList<>();
        List<Map<String, String>> jzList = new ArrayList<>();
        List<Map<String, String>> jkList = new ArrayList<>();
        for (JzInfoTable ele : jzInfoTables) {
            Map<String, String> jzMap = new HashMap<>();
            jzMap.put("type", "jz");
            jzMap.put("jzName", ele.getJzName());
            jzMap.put("jzId", ele.getId());
            jzMap.put("jzXpoint", ele.getJkXpoint());
            jzMap.put("jzYpoint", ele.getJkYpoint());
            jzList.add(jzMap);

            List<JcdInfoTable> jcdInfoTables = getJcdListSourceDao.findByJzId(ele.getId());
            for (JcdInfoTable item : jcdInfoTables) {
                Map<String, String> jcdMap = new HashMap<>();
                jcdMap.put("type", "jcd");
                jcdMap.put("jcdName", item.getJcdName());
                jcdMap.put("jcdId", item.getId());
                jcdMap.put("jcdXpoint", item.getJcdXpoint());
                jcdMap.put("jcdYpoint", item.getJcdYpoint());
                jcdMap.put("jcValue", String.valueOf(item.getUpdateValue()));
                String d = sdf.format(item.getUpdateTime());
                jcdMap.put("updateTime", d);
                jcdList.add(jcdMap);
            }


            List<JcEventRightPanelResponse> liantongxingJcEvents = jcEventService.list(JcEvent.builder().jzId(ele.getId()).jcType("连通性").build());
            for (JcEventRightPanelResponse item : liantongxingJcEvents) {
                Map<String, String> xldMap = new HashMap<>();
                xldMap.put("type", "xld");
                xldMap.put("xldId", item.getJcEventId());
                xldMap.put("xldcXpoint", item.getJcXpoint());
                xldMap.put("xldcYpoint", item.getJcYpoint());
                xldMap.put("jcValue", String.valueOf(item.getJcValue()));
                xldMap.put("startTime", item.getJcTime());
                xldMap.put("dealSection", "未处理");
                xlList.add(xldMap);
            }


            List<JcEventRightPanelResponse> hkJcEvents = jcEventService.list(JcEvent.builder().jzId(ele.getId()).jcType("地表示踪剂").build());
            for (JcEventRightPanelResponse item : hkJcEvents) {
                Map<String, String> xldMap = new HashMap<>();
                xldMap.put("type", "hk");
                xldMap.put("hkId", item.getJcEventId());
                xldMap.put("hkXpoint", item.getJcXpoint());
                xldMap.put("hkYpoint", item.getJcYpoint());
                xldMap.put("jcValue", String.valueOf(item.getJcValue()));
                xldMap.put("startTime", item.getJcTime());
                xldMap.put("dealSection", "未处理");
                hkList.add(xldMap);
            }

            List<JcEventRightPanelResponse> ckJcEvents = jcEventService.list(JcEvent.builder().jzId(ele.getId()).jcType("井口示踪剂").build());
            for (JcEventRightPanelResponse item : ckJcEvents) {
                Map<String, String> xldMap = new HashMap<>();
                xldMap.put("type", "ck");
                xldMap.put("ckId", item.getJcEventId());
                xldMap.put("ckXpoint", item.getJcXpoint());
                xldMap.put("ckYpoint", item.getJcYpoint());
                xldMap.put("jcValue", String.valueOf(item.getJcValue()));
                xldMap.put("startTime", item.getJcTime());
                xldMap.put("dealSection", "未处理");
                ckList.add(xldMap);
            }


            List<JcEventRightPanelResponse> jkJcEvents = jcEventService.list(JcEvent.builder().jzId(ele.getId()).jcType("环空示踪剂").build());
            for (JcEventRightPanelResponse item : jkJcEvents) {
                Map<String, String> xldMap = new HashMap<>();
                xldMap.put("type", "jk");
                xldMap.put("jkId", item.getJcEventId());
                xldMap.put("jkXpoint", item.getJcXpoint());
                xldMap.put("jkYpoint", item.getJcYpoint());
                xldMap.put("jcValue", String.valueOf(item.getJcValue()));
                xldMap.put("startTime", item.getJcTime());
                xldMap.put("dealSection", "未处理");
                jkList.add(xldMap);
            }

        }
        res.put("jzList", jzList);
        res.put("jcdList", jcdList);
        res.put("xlList", xlList);
        res.put("ckList", ckList);
        res.put("hkList", hkList);
        res.put("jkList", jkList);
        return res;
    }

    @Override
    public Map<String, List<Map<String, String>>> getSpecJzView(String id, String kqId) {
        Map<String, List<Map<String, String>>> res = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        List<Map<String, String>> jcdList = new ArrayList<>();
        List<Map<String, String>> xlList = new ArrayList<>();
        List<Map<String, String>> ckList = new ArrayList<>();
        List<Map<String, String>> hkList = new ArrayList<>();
        List<Map<String, String>> jzList = new ArrayList<>();
        List<Map<String, String>> jkList = new ArrayList<>();
        List<JzInfoTable> jzInfoTables = getJzListSourceDao.findByKqId(kqId);
        for (JzInfoTable ele : jzInfoTables) {
            Map<String, String> jzMap = new HashMap<>();
            jzMap.put("type", "jz");
            jzMap.put("jzName", ele.getJzName());
            jzMap.put("jzId", ele.getId());
            jzMap.put("jzXpoint", ele.getJkXpoint());
            jzMap.put("jzYpoint", ele.getJkYpoint());
            jzList.add(jzMap);
        }

        List<JcdInfoTable> jcdInfoTables = getJcdListSourceDao.findByJzId(id);
        for (JcdInfoTable item : jcdInfoTables) {
            Map<String, String> jcdMap = new HashMap<>();
            jcdMap.put("type", "jcd");
            jcdMap.put("jcdId", item.getId());
            jcdMap.put("jcdName", item.getJcdName());
            jcdMap.put("jcdXpoint", item.getJcdXpoint());
            jcdMap.put("jcdYpoint", item.getJcdYpoint());
            jcdMap.put("jcValue", String.valueOf(item.getUpdateValue()));
            String d = sdf.format(item.getUpdateTime());
            jcdMap.put("updateTime", d);
            jcdList.add(jcdMap);
        }


        List<JcEventRightPanelResponse> liantongxingJcEvents = jcEventService.list(JcEvent.builder().jzId(id).jcType("连通性").build());
        for (JcEventRightPanelResponse item : liantongxingJcEvents) {
            Map<String, String> xldMap = new HashMap<>();
            xldMap.put("type", "xld");
            xldMap.put("xldId", item.getJcEventId());
            xldMap.put("xldcXpoint", item.getJcXpoint());
            xldMap.put("xldcYpoint", item.getJcYpoint());
            xldMap.put("jcValue", String.valueOf(item.getJcValue()));
            xldMap.put("startTime", item.getJcTime());
            xldMap.put("dealSection", "未处理");
            xlList.add(xldMap);
        }


        List<JcEventRightPanelResponse> hkJcEvents = jcEventService.list(JcEvent.builder().jzId(id).jcType("地表示踪剂").build());
        for (JcEventRightPanelResponse item : hkJcEvents) {
            Map<String, String> xldMap = new HashMap<>();
            xldMap.put("type", "hk");
            xldMap.put("hkId", item.getJcEventId());
            xldMap.put("hkXpoint", item.getJcXpoint());
            xldMap.put("hkYpoint", item.getJcYpoint());
            xldMap.put("jcValue", String.valueOf(item.getJcValue()));
            xldMap.put("startTime", item.getJcTime());
            xldMap.put("dealSection", "未处理");
            hkList.add(xldMap);
        }

        List<JcEventRightPanelResponse> ckJcEvents = jcEventService.list(JcEvent.builder().jzId(id).jcType("井口示踪剂").build());
        for (JcEventRightPanelResponse item : ckJcEvents) {
            Map<String, String> xldMap = new HashMap<>();
            xldMap.put("type", "ck");
            xldMap.put("ckId", item.getJcEventId());
            xldMap.put("ckXpoint", item.getJcXpoint());
            xldMap.put("ckYpoint", item.getJcYpoint());
            xldMap.put("jcValue", String.valueOf(item.getJcValue()));
            xldMap.put("startTime", item.getJcTime());
            xldMap.put("dealSection", "未处理");
            ckList.add(xldMap);
        }


        List<JcEventRightPanelResponse> jkJcEvents = jcEventService.list(JcEvent.builder().jzId(id).jcType("环空示踪剂").build());
        for (JcEventRightPanelResponse item : jkJcEvents) {
            Map<String, String> xldMap = new HashMap<>();
            xldMap.put("type", "jk");
            xldMap.put("jkId", item.getJcEventId());
            xldMap.put("jkXpoint", item.getJcXpoint());
            xldMap.put("jkYpoint", item.getJcYpoint());
            xldMap.put("jcValue", String.valueOf(item.getJcValue()));
            xldMap.put("startTime", item.getJcTime());
            xldMap.put("dealSection", "未处理");
            jkList.add(xldMap);
        }

        res.put("jzList", jzList);
        res.put("jcdList", jcdList);
        res.put("xlList", xlList);
        res.put("ckList", ckList);
        res.put("hkList", hkList);
        res.put("jkList",jkList);
        return res;
    }

}
